package com.czz.manage.service.impl;

import com.czz.manage.mapper.*;
import com.czz.manage.service.DailyService;
import com.czz.manage.utils.DateUtil;
import com.czz.manage.vo.ChartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: DailyServiceImpl
 * @Author Czz
 * @Date 2020/3/12
 * @Time 11:18
 **/
@Service
public class DailyServiceImpl implements DailyService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CourseConsultMapper consultMapper;

    @Override
    public List<Map<String, String>> getAdminDailyChart(String begin, String end, String type) {
        List<ChartVo> dataList = new ArrayList<>();

        switch (type) {
            case "user":
                dataList = userMapper.getAdminUserDaily(begin,end);
                break;
            case "teacher":
                dataList = teacherMapper.getAdminTeacherDaily(begin,end);
                break;
            case "course":
                dataList = courseMapper.getAdminCourseDaily(begin,end);
                break;
            case "order":
                dataList = orderMapper.getAdminOrderDaily(begin,end);
                break;
            default:
                break;
        }
        //日期列表
        List<String> dateList = DateUtil.findDates(begin, end);

        List<Map<String,String>> mapList = new ArrayList<>();

        //填充时间
        dataList.forEach(data -> {
            Map<String,String> temp = new HashMap<>();
            temp.put("date",data.getDate());
            temp.put("count",data.getCount());
            mapList.add(temp);
        });

        Boolean flag = false;
        for (int i = 0; i < dateList.size(); i++){
            flag = false;
            for (Map<String, String> list : mapList) {
                if (dateList.get(i).equals(list.get("date"))) {
                    flag = true;
                    break;
                }
            }
            if(!flag){
                final String date = dateList.get(i);
                mapList.add(i,new HashMap<String,String>(){{
                    put("date", date);
                    put("count", "0");
                }});
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, String>> getTeacherDailyChart(String teacherId, String begin, String end, String type) {
        List<ChartVo> dataList = new ArrayList<>();

        switch (type) {
            case "course":
                dataList = courseMapper.getTeacherCourseDaily(teacherId,begin,end);
                break;
            case "consult":
                dataList = consultMapper.getTeacherConsultDaily(teacherId,begin,end);
                break;
            case "order":
                dataList = orderMapper.getTeacherOrderDaily(teacherId,begin,end);
                break;
            case "money":
                dataList = orderMapper.getTeacherMoneyDaily(teacherId,begin,end);
                break;
            default:
                break;
        }
        //日期列表
        List<String> dateList = DateUtil.findDates(begin, end);

        List<Map<String,String>> mapList = new ArrayList<>();

        //填充时间
        dataList.forEach(data -> {
            Map<String,String> temp = new HashMap<>();
            temp.put("date",data.getDate());
            temp.put("count",data.getCount());
            mapList.add(temp);
        });

        Boolean flag = false;
        for (int i = 0; i < dateList.size(); i++){
            flag = false;
            for (Map<String, String> list : mapList) {
                if (dateList.get(i).equals(list.get("date"))) {
                    flag = true;
                    break;
                }
            }
            if(!flag){
                final String date = dateList.get(i);
                mapList.add(i,new HashMap<String,String>(){{
                    put("date", date);
                    put("count", "0");
                }});
            }
        }
        return mapList;
    }
}
